package cn.pugle.oj.leetcode;

import cn.pugle.oj.catalog.DynamicProblem;
import cn.pugle.oj.catalog.Greedy;

/**
 * https://leetcode.com/problems/maximum-subarray/
 *
 * @author tzp
 * @since 2020/9/17
 */
public class LC53 implements DynamicProblem, Greedy {
    public int maxSubArray(int[] nums) {
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < nums.length; i++) {
            int now = 0;
            for (int j = i; j < nums.length; j++) {
                now = nums[j] + now;
                max = Math.max(max, now);
            }
        }
        return max;
    }

    public static void main(String[] args) {
        System.out.println(new LC53().maxSubArray(new int[]{1, 2, 3, 4}));
        System.out.println(new LC53().maxSubArray(new int[]{1, -2, 100, -4}));
        System.out.println(new LC53().maxSubArray(new int[]{-2, 1, -3, 4, -1, 2, 1, -5, 4}));
    }
}
